﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_AdminStudAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string studId = txtStudID.Text.Trim();
        if (studId != "")
        {
            DataTable dt_view_studId = User_Control.getStudentInfoInViewByStudId(studId);
            if (dt_view_studId.Rows.Count > 0)
            {
                DataTable dt_studId = User_Control.getStudentInfoByStudId(studId);
                if (dt_studId.Rows.Count > 0)
                {
                    errorTips.Visible = true;
                    errorTips.Text = "存在该同学!";
                    gv.Visible = true;
                    gv_view.Visible = false;
                    gv.DataSource = dt_studId;
                    gv.DataBind();
                }
                else
                {                    
                    errorTips.Visible = true;
                    errorTips.Text = "不存在该同学,请点击选择添加学生!";
                    gv.Visible = false; 
                    gv_view.Visible = true;
                    gv_view.DataSource = dt_view_studId;
                    gv_view.DataBind();
                }
            }
            else
            {
                DataTable dt_view_idCard = User_Control.getStudInfoInViewByIdCard(studId);
                if (dt_view_idCard.Rows.Count > 0)
                {
                    DataTable dt_studIdCard = User_Control.getStudInfoByIdCard(studId);
                    if (dt_studIdCard.Rows.Count > 0)
                    {
                        errorTips.Visible = true;
                        errorTips.Text = "存在该学生";
                        gv.Visible = true;
                        gv_view.Visible = false;
                        gv.DataSource = dt_studIdCard;
                        gv.DataBind();
                    }
                    else
                    {
                        
                        errorTips.Visible = true;
                        errorTips.Text = "不存在该同学,请点击添加学生!";
                        gv.Visible = false;
                        gv_view.Visible = true;
                        gv_view.DataSource = dt_view_idCard;
                        gv_view.DataBind();
                    }
                }
                else
                {
                    errorTips.Visible = true;
                    errorTips.Text = "不存在该学生，请点击管理员添加学生!";
                }
            }
        }
        else
        {
            errorTips.Visible = true;
            errorTips.Text = "输入为空!";
            gv.Visible = false;
        }
    }

    protected void addStudent_Click(object sender, EventArgs e)
    {
        string studName = txtName.Text.Trim();
        string idCard = txtIdCard.Text.Trim();
        string sex = string.Empty;
        if (rbMan.Checked == true)
        {
            sex = "男";
        }
        else if (rbMale.Checked == true)
        {
            sex = "女";
        }
        string studId = txtStudentId.Text.Trim();
        string from = ltFrom.Text.Trim();
        string deptName = txtDeptName.Text.Trim();
        string className = txtClassName.Text.Trim();
        string enterYear = txtEnterYear.Text.Trim();
        if (studName != "" && idCard != "")
        {
            if (User_Control.insertNewStudentInfo(studName, idCard, sex, studId, from, deptName, className, enterYear,""))
            {
                addTips.Visible = true;
                addTips.Text = "添加成功";
            }
            else
            {
                addTips.Visible = true;
                addTips.Text = "Fail,检查数据库!";
            }
        }
        else
        {
            addTips.Visible = true;
            addTips.Text = "缺少主要信息";
        }
    }

    protected void gv_view_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.CompareTo("add") == 0)
        {
            string studId = txtStudID.Text.Trim();
            string rtn = User_Control.insertStudentInfo(studId);
            if (rtn == string.Empty)
            {
                errorTips.Visible = true;
                errorTips.Text = "添加成功!";
            }
            else
            {
                errorTips.Visible = true;
                errorTips.Text = rtn;
            }
        }
    }
}